import api from "../../../common/api";
import { gioCooperationLog } from "../../../common/gioHandler";
const app = getApp();
Component({
  /**
   * 组件的属性列表
   */
  properties: {
    houseInfo:{
      type:Array,
      value:''
    },
    hasMoreList:{
      type:Boolean,
      value:''
    }
  },

      //监听组件数据
      observers: {
        houseInfo(){
          let houseInfo = [...this.data.houseInfo]
          for(let i in houseInfo){
            // 开始计算什么时间来过
            let newTime = new Date().getTime()
            let LoginDate = houseInfo[i].LoginDate = (newTime - houseInfo[i].lastLoginDate) / 1000 //单位秒
            if(LoginDate <= 60){
              houseInfo[i].lookTime = '刚刚来过'
            }
            if(60 < LoginDate && LoginDate < 3600){
              houseInfo[i].lookTime = parseInt(LoginDate/60) +"分钟前来过"
            }
            if(3600 <= LoginDate && LoginDate < 86400){
              houseInfo[i].lookTime = parseInt(LoginDate/3600) +"小时前来过"
            }
            if(86400 <= LoginDate && LoginDate < 2592000){
              houseInfo[i].lookTime = parseInt(LoginDate/86400) +"天前来过"
            }
            if(2592000 <= LoginDate){
              houseInfo[i].lookTime = "一个月前来过"
            }

            // 开始计算响应时间 &lt <    &gt >
            let averageIMReplyTime = houseInfo[i].averageIMReplyTime
            if(averageIMReplyTime<= 5){
              houseInfo[i].playTime = '<5min'
            }
            if(5<averageIMReplyTime&&averageIMReplyTime <= 30){
              houseInfo[i].playTime = '<30min'
            }
            if(30<averageIMReplyTime&&averageIMReplyTime <= 60){
              houseInfo[i].playTime = '<60min'
            }
            if(60<averageIMReplyTime){
              houseInfo[i].playTime = '>1h'
            }

            // 开始计算接通率
            let callSuccessRatio = houseInfo[i].callSuccessRatio
            if(callSuccessRatio){
              if(callSuccessRatio < 60){
                houseInfo[i].playPhone = '<60%'
              }
              if(60 <= callSuccessRatio&&callSuccessRatio < 80){
                houseInfo[i].playPhone = '>60%'
              }
              if(80 <= callSuccessRatio&&callSuccessRatio < 90){
                houseInfo[i].playPhone = '>80%'
              }
              if(90 <= callSuccessRatio){
                houseInfo[i].playPhone = '>90%'
              }
            }
          }
          this.setData({
            _houseInfo: houseInfo,
          })
        }
      },

  /**
   * 组件的初始数据
   */
  data: {
    _houseInfo:'',
    toUserId:'',//对方的用户id
    isVmShow:false,//控制打电话弹窗的显示
  },

    /**
   * 组件的生命周期
   */
  lifetimes: {
    attached: function () {
      // 在组件实例被从页面节点树加载时执行
    },
    detached: function () {
      // 在组件实例被从页面节点树移除时执行
    },
  },

  /**
   * 组件的方法列表
   */
  methods: {
    // 去聊天
    goToLine(e){
      app.globalData.loginPromise.then(({ sessionUser }) => {
        gioCooperationLog('IM_click', sessionUser.id, '首页推荐经纪人');
      })
      const unionid = e.currentTarget.dataset.unionid
      wx.navigateTo({
        url: `/pages/im/imMessagelist/imMessagelist?unionid=${unionid}&pageFrom=首页推荐经纪人`,
      })
    },

    // 拨打电话
    goToPhone(e){
      this.setData({
        isVmShow:true,
        toUserId: e.currentTarget.dataset.userid
      })
      app.globalData.loginPromise.then(({ sessionUser }) => {
        gioCooperationLog('cooperation_phone_click', sessionUser.id, '首页-推荐经纪人');
      })
    },
    handleVmVisible(e) {
      this.setData({
        isVmShow: e.detail.visible
      });
    },


    // 查看更多经纪人
    lookMore(){
      this.triggerEvent('lookMore')
    }

  }
})
